#!/bin/bash

show_help(){
    echo
    echo " -----------------------------------------------"
    echo "NAME"
    echo "      sample_aa_update_ubuntu.sh"
    echo
    echo "SYNOPSIS"
    echo "      sample_aa_update_ubuntu.sh [OPTION]... --target-os=<name> --input-dir=<name> --output-dir=<name> --machine-design-name=<name> --software-cluster-list=<swcls>"
    echo
    echo "DESRIPTION"
    echo "      Rename and reconfigure collected files."
    echo
    echo "      --target-os, -tos"
    echo "          Specify the target OS to be built."
    echo
    echo "      --input-dir, -indir"
    echo "          Input for the necessary files from the specified path."
    echo
    echo "      --output-dir, -outd"
    echo "          adaptive application machine name."
    echo
    echo "      --arxml-machine-name, -amn"
    echo "          MACHINE name."
    echo
    echo "      --machine-design-name, -mdn"
    echo "          The SHORT-NAME of MACHINE-DESIGN defined in ARXML."
    echo
    echo
    echo "ATTENTION"
    echo "      Do not place more than two files that contain the names _sd.json and _gw.json in the input path."
    echo
    echo "EXAMPLE"
    echo "      sample_aa_update_ubuntu.sh  --target-os=Ubuntu --input-dir=/app --output-dir=/ap_home/skeleton --machine-design-name=MachineDesign_MA1"
    echo
    echo " -----------------------------------------------"
    exit 0
}

show_invalid_option(){
    echo ""
    echo "Invalid option!"
    show_help
}

# parse options
for i in $*; do
    case "$i" in
        -tos=* | --target-os=*)
            TARGET_OS=${i#-*=}
            ;;
        -indir=* | --input-dir=*)
            INPUT_DIR=${i#-*=}
            ;;
        -outd=* | --output-dir=*)
            OUT_DIR=${i#-*=}
            ;;
        -mdn=* | --machine-design-name=*)
            MACHINE_DESIGN_NAME=${i#-*=}
            ;;
        -h | --help)
            show_help
            ;;
        *)
            echo "unknown option '$i'"
            show_invalid_option
            usage 1
            ;;
    esac
done

if [ -z "$TARGET_OS" ]; then
    echo "Specify the target OS to be built: -tos/--target-os"
    exit 0
fi

if [ -z "$INPUT_DIR" ]; then
    echo "Input for the necessary files from the specified path: -indir/--input-dir"
    exit 0
fi

if [ -z "$OUT_DIR" ]; then
    echo "the following arguments are required: -outd/--output-dir"
    exit 0
fi

if [ -z "$MACHINE_DESIGN_NAME" ]; then
    echo "the following arguments are required: -mdn/--machine-design-name"
    exit 0
fi


if [ `echo $OUT_DIR | grep 'skeleton'` ] ; then
    SKELETON_TEMP=${OUT_DIR}

    if [ -e ${SKELETON_TEMP}/opt/vrte/dia-diagnostic-manager/etc/dm_config_ma_Skeleton.json ] ; then
        echo "[INFO] deploy json files ${SKELETON_TEMP}/opt/vrte/dia-diagnostic-manager/etc/dm_config.json"
        mv ${SKELETON_TEMP}/opt/vrte/dia-diagnostic-manager/etc/dm_config_ma_Skeleton.json ${SKELETON_TEMP}/opt/vrte/dia-diagnostic-manager/etc/dm_config.json.orig && \
            sed s/vx0/eth0/g < ${SKELETON_TEMP}/opt/vrte/dia-diagnostic-manager/etc/dm_config.json.orig > ${SKELETON_TEMP}/opt/vrte/dia-diagnostic-manager/etc/dm_config.json
    fi

    if [ -e ${SKELETON_TEMP}/opt/vrte/nm-network-manager/bin/MachineDesign_MA1_nm_config.json ] ; then
        echo "[INFO] rename json files ${SKELETON_TEMP}/opt/vrte/nm-network-manager/bin/MachineDesign_MA1_nm_config.json -> nm_config.json"
        mv ${SKELETON_TEMP}/opt/vrte/nm-network-manager/bin/MachineDesign_MA1_nm_config.json ${SKELETON_TEMP}/opt/vrte/nm-network-manager/bin/nm_config.json
    fi
fi

if [ `echo $OUT_DIR | grep 'proxy'` ] ; then
    PROXY_TEMP=${OUT_DIR}

    if [ -e ${SKELETON_TEMP}/opt/vrte/dia-diagnostic-manager/etc/dm_config_ma_Skeleton.json ] ; then
        echo "[INFO] deploy json files ${PROXY_TEMP}/opt/vrte/dia-diagnostic-manager/etc/dm_config.json"
        mv ${PROXY_TEMP}/opt/vrte/dia-diagnostic-manager/etc/dm_config_ma_Skeleton.json ${PROXY_TEMP}/opt/vrte/dia-diagnostic-manager/etc/dm_config.json.orig && \
            sed s/vx0/eth0/g < ${PROXY_TEMP}/opt/vrte/dia-diagnostic-manager/etc/dm_config.json.orig > ${PROXY_TEMP}/opt/vrte/dia-diagnostic-manager/etc/dm_config.json
    fi

    if [ -e ${PROXY_TEMP}/opt/vrte/nm-network-manager/bin/MachineDesign_MA2_nm_config.json ] ; then
        echo "[INFO] rename json files ${PROXY_TEMP}/opt/vrte/nm-network-manager/bin/MachineDesign_MA2_nm_config.json -> nm_config.json"
        mv ${PROXY_TEMP}/opt/vrte/nm-network-manager/bin/MachineDesign_MA2_nm_config.json ${PROXY_TEMP}/opt/vrte/nm-network-manager/bin/nm_config.json
        sed -i 's/\.111\.11/\.111\.20/' ${PROXY_TEMP}/opt/vrte/nm-network-manager/bin/nm_config.json
    fi

fi

echo -e "\033[35m### $(basename $0) finished ###\033[m"

exit
